package lj.courseinterect.dao;

import lj.courseinterect.dao.i.ExamInfoDao;
import lj.courseinterect.dao.i.ExamItemDao;
import lj.courseinterect.entity.ExamInfo;
import lj.courseinterect.entity.ExamItem;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.List;

public class JpaExamItemDao extends JpaDao<ExamItem, Long> implements ExamItemDao{

    public JpaExamItemDao() {
        super(ExamItem.class);
    }


    
    @Override
    @Transactional(readOnly = true)

    public List<ExamItem> findByExaId(String exaId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<ExamItem> criteriaQuery = builder.createQuery(this.entityClass);

        Root<ExamItem> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("exaId");
        criteriaQuery.where(builder.equal(namePath, exaId));


        TypedQuery<ExamItem> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<ExamItem> examItems = typedQuery.getResultList();

        if (examItems.isEmpty()) {
            return null;
        }
        return examItems;
    }
}
